const jwtUtil = require('../utils/jwtUtils')
module.exports = class user_dao extends require('../model/user_mod') {
  /**
   * @description: 发起登录请求
   * @param {*} req
   * @param {*} res
   * @return {*}
   */
  static async Login(req, res) {
    let {
      username,
      password,
      type
    } = req.body
    let loginData = await this.LoginUser(username, password, type)
    if (loginData.length > 0) {
      let token = jwtUtil.sign({
        id: loginData[0].id,
        username: loginData[0].username,
        head: loginData[0].head,
        type: loginData[0].type,
        classes: loginData[0].classes,
        address: loginData[0].address,
        create_time: loginData[0].create_time,
        update_time: loginData[0].update_time,
        sex: loginData[0].sex,
        password: loginData[0].password
      }, global.globalKey, 3600 * 12)
      res.json({
        userInfo: loginData[0],
        status: 200,
        statusText: 'ok',
        token
      })
    } else {
      res.json({
        status: 500,
        statusText: '账号或密码错误~~'
      })
    }
  }

  /**
   * @description: 根据加密的token解析用户信息
   * @param {*} req
   * @param {*} res
   * @return {*}
   */
  static async getUserDataByToken(req, res) {
    let results = await jwtUtil.verifysync(req.query.token, global.globalKey)
    res.json(results)
  }

  /**
   * @description: 获取一定数量的某种用户的信息
   * @param {*} req
   * @param {*} res
   * @return {*}
   */
  static async getUsersByTypePage(req, res) {
    const {
      type,
      currPage,
      pageNum
    } = req.query
    let data = await this.getUsersByTypePageMod(type, currPage, pageNum)
    let total = await this.getUsersByTypePageTotal(type)
    res.json({
      data,
      total: total[0].count
    })
  }

  /**
   * @description: 删除用户信息及关联的阅读表信息
   * @param {*} req
   * @param {*} res
   * @return {*}
   */
  static async delUserdata(req, res) {
    let results = await this.delUserdataMod(req.query.u_id)
    let results2 = await this.delRead(req.query.u_id)

    res.send(results + results2)
  }

  /**
   * @description: 修改用户信息
   * @param {*} req
   * @param {*} res
   * @return {*}
   */
  static async upUserdata(req, res) {
    let {
      u_id,
      username,
      type,
      sex,
      address,
      password,
    } = req.body
    let results = await this.upUserdataMod(u_id, username, password, sex, address, type)
    res.json(results)
  }

  /**
   * @description: 增加用户信息
   * @param {*} req
   * @param {*} res
   * @return {*}
   */
  static async addUserData(req, res) {
    let {
      id,
      username,
      password,
      type,
      sex,
      address,
      classes,
    } = req.body
    let results = await this.addUserDataMod(id, type, username, password, sex, address, classes)
    res.json(results)
  }

  /* ----------------修改个人信息--------------------- */
  // 修改密码
  static async updatePassword(req, res) {
    let {
      token,
      newpassword,
      oldpassword
    } = req.body
    let verify = await jwtUtil.verifysync(token, global.globalKey)
    if (verify.error == "error") {
      res.json(verify.msg)
    } else {
      let u_id = verify.id
      let results = await this.updatePasswordMod(u_id, oldpassword, newpassword)
      res.json(results)
    }

  }

  // 修改头像
  static async updateAvatar(req, res) {
    let {
      token,
      head
    } = req.body
    let verify = await jwtUtil.verifysync(token, global.globalKey)
    if (verify.error == "error") {
      res.json(verify.msg)
    } else {
      let id = verify.id
      let results = await this.updateAvatarMod(id, head)
      res.json({
        results
      })
    }
  }

}